package system

import (
	"parking-lot-go-dingding/model"
	"time"
)

type DingtalkSystemCorpStruct struct {
	Id        uint      `json:"id" gorm:"type:int(10) unsigned auto_increment; NOT NULL; primary_key"`
	CorpId    string    `json:"corp_id" gorm:"type:varchar(50)"`
	CorpName  string    `json:"corp_name" gorm:"type:varchar(255); COMMENT:'企业名称'"`
	Money     float64   `json:"money" gorm:"type:decimal(10,2); COMMENT:'企业余额'"`
	CreatedAt time.Time `json:"created_at" gorm:"type:datetime"`
	UpdatedAt time.Time `json:"updated_at" gorm:"type:datetime"`
}

func (c *DingtalkSystemCorpStruct) TableName() string {
	return "dingtalk_system_corp"
}

func (c *DingtalkSystemCorpStruct) First(corpId string) (DingtalkSystemCorpStruct, error) {
	var dingtalkSystemCorpStruct DingtalkSystemCorpStruct
	db := model.Db
	if err := db.
		Debug().
		Table(c.TableName()).
		Where("corp_id", corpId).
		First(&dingtalkSystemCorpStruct).
		Error; err != nil {
		return dingtalkSystemCorpStruct, err
	}
	return dingtalkSystemCorpStruct, nil
}

func (c *DingtalkSystemCorpStruct) Create(corpId, corpName string) error {
	db := model.Db
	if err := db.Table(c.TableName()).
		Create(map[string]interface{}{
			"corp_id":    corpId,
			"corp_name":  corpName,
			"created_at": time.Now(),
			"updated_at": time.Now(),
		}).Error; err != nil {
		return err
	}
	return nil
}

func (c *DingtalkSystemCorpStruct) Delete(corpid string) error {
	db := model.Db
	return db.Table(c.TableName()).Where("corp_id", corpid).Delete(&DingtalkSystemCorpStruct{}).Error
}
